package main

import (
	"github.com/streadway/amqp"
	"log"
	"fmt"
	"time"
)

func isError(err error, msg string) {
	if err != nil{
		log.Fatal(fmt.Sprintf("%s: %s", msg, err))
		panic(fmt.Sprintf("%s:%s", msg, err))
	}
}

func main() {
	conn, err := amqp.Dial("amqp://localhost")
	isError(err, "failed to connect to rabbitmq")
	defer conn.Close()

	ch, _ := conn.Channel()
	q, err := ch.QueueDeclare(
		"hello", // 队列名字
		true,	// 是否持久化队列
		false,
		false,
		false,
		nil,
	)

	isError(err, "Failed to declare a queue")

	date := time.Now().String()
	err = ch.Publish(
		"",
		q.Name,
		false,
		false,
		amqp.Publishing{
			DeliveryMode:amqp.Persistent,
			ContentType:"text/plain",
			Body:[]byte("hello" + date),
		})
	isError(err, "Failed to Publish a message")

}